package com.forum.impl.photo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.entity.ApiResult;
import com.common.utils.LocalContext;
import com.forum.mapper.param.PhotoDetailResponseParam;
import com.forum.mapper.param.RecommendPhotoResponseParam;
import com.forum.mapper.photo.PhotoUserLikeMapper;
import com.forum.mapper.photo.PhotoWallMapper;
import com.forum.pojo.photo.PhotoUserLike;
import com.forum.pojo.photo.PhotoWall;
import com.forum.service.photo.PhotoWallService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Tsing
 */
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Service
public class PhotoWallServiceImpl extends ServiceImpl<PhotoWallMapper, PhotoWall> implements PhotoWallService {
    public final PhotoWallMapper photoWallMapper;
    public final PhotoUserLikeMapper photoUserLikeMapper;

    // Get Photo By Category and Current and Size
    @Override
    public ApiResult<?> getPhoto(int page, int size, int categoryId) {
        List<RecommendPhotoResponseParam> recommendPhoto = photoWallMapper.getRecommendPhoto(((page-1) * size), size,categoryId);

        List<RecommendPhotoResponseParam> recommendPhoto1 = photoWallMapper.getRecommendPhoto(null, null,null);

        Map<String,Object> map = new LinkedHashMap<>();

        map.put("total", recommendPhoto1.size());

        map.put("list", recommendPhoto);

        return ApiResult.success(map);
    }
    // 获取推荐的照片
    @Override
    public ApiResult<?> getRecommendPhoto(Integer page, Integer size) {
        /**
         *  获取需求 ： 多表联查 照片墙+照片分类+用户信息(用户账户 用户昵称 用户 编号)
         *  sql条件 ： 时间排序从大到小 + 分页查询
         */
        List<RecommendPhotoResponseParam> recommendPhoto = photoWallMapper.getRecommendPhoto(((page-1) * size), size,null);
        List<RecommendPhotoResponseParam> recommendPhoto1 = photoWallMapper.getRecommendPhoto(null, null,null);
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("total", recommendPhoto1.size());
        map.put("list",recommendPhoto);
        return ApiResult.success(map);
    }
    // 获取一组照片详情
    @Override
    public ApiResult<?> getPhotoDetail(Integer photoWallId) {
        PhotoDetailResponseParam photoDetail = photoWallMapper.getPhotoDetail(photoWallId);
        // 查询用户点赞状态
        PhotoUserLike photoUserLike = photoUserLikeMapper.selectOne(new LambdaQueryWrapper<PhotoUserLike>().eq(PhotoUserLike::getForumUserId, LocalContext.getCurrentUserId()));
        if (!Objects.isNull(photoUserLike)){
            photoDetail.setLikeState(true);
        }
        return ApiResult.success(photoDetail);
    }
    // Post a set of images 发布一组图片
    public ApiResult<?> submitImages(@RequestBody PhotoWall photoWall){
        int insert = photoWallMapper.insert(photoWall);
        return  insert>=1?ApiResult.success():ApiResult.fail(500, "图片发布失败");
    }
    @Transactional
    @Override
    public ApiResult<?> likePhoto(boolean likeState, Integer photoWallId) {
        // 用户ID
        Integer userId = LocalContext.getCurrentUserId();
        // 查询用户点赞表中有无数据
        LambdaQueryWrapper<PhotoUserLike> userLikeDetail = new LambdaQueryWrapper<>();
        userLikeDetail.eq(PhotoUserLike::getForumUserId,userId).eq(PhotoUserLike::getPhotoWallId,photoWallId);
        PhotoUserLike photoUserLike1 = photoUserLikeMapper.selectOne(userLikeDetail);
        if (likeState && Objects.isNull(photoUserLike1)){
            // 点赞 => 添加到点赞表中 + 照片墙点赞数量+1
            PhotoUserLike photoUserLike = new PhotoUserLike();
            photoUserLike.setForumUserId(userId);
            photoUserLike.setPhotoWallId(photoWallId);
            photoUserLikeMapper.insert(photoUserLike);
            // 照片墙数据量+1
            LambdaQueryWrapper<PhotoWall> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PhotoWall::getId, photoWallId);
            PhotoWall photoWall = photoWallMapper.selectOne(wrapper);
            photoWall.setLikeSum(photoWall.getLikeSum()+1);
            // 数据持久化
            photoWallMapper.update(photoWall, wrapper);
            return ApiResult.success(200,"点赞成功",null);
        }
        if (!likeState && !Objects.isNull(userLikeDetail)){
            // 取消点赞 => 移除点赞表中数据  + 照片墙点赞数据量-1
            LambdaQueryWrapper<PhotoUserLike> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PhotoUserLike::getForumUserId,userId).eq(PhotoUserLike::getPhotoWallId,photoWallId);
            int delete = photoUserLikeMapper.delete(wrapper);
            if (delete>0){
                // 删除成功 =>代表有数据 => 减少点赞数量
                LambdaQueryWrapper<PhotoWall> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(PhotoWall::getId, photoWallId);
                PhotoWall photoWall = photoWallMapper.selectOne(wrapper1);
                photoWall.setLikeSum(photoWall.getLikeSum()-1);
                photoWallMapper.update(photoWall, wrapper1);
                return ApiResult.success(200,"已取消点赞",null);
            }
        }
        return ApiResult.success(200,"重复操作",null);
    }

    // Get My Photo Walls
    @Override
    public ApiResult<?> getMyPhotoWalls(int page,int size) {
        List<RecommendPhotoResponseParam> myPhotoWalls = photoWallMapper.getMyPhotoWalls(LocalContext.getCurrentUserId(), ((page-1) * size), size);
        List<RecommendPhotoResponseParam> myPhotoWalls2 = photoWallMapper.getMyPhotoWalls(LocalContext.getCurrentUserId(), null, null);
        Map<String,Object> map = new LinkedHashMap<>();
        map.put("total", myPhotoWalls2.size());
        map.put("list",myPhotoWalls);
        return ApiResult.success(map);
    }
}
